package cn.kgc.ssm.service.impl;

import cn.kgc.ssm.entity.Active;
import cn.kgc.ssm.mapper.ActiveMapper;
import cn.kgc.ssm.service.ActiveService;
import cn.kgc.ssm.utils.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/3/7
 * @description
 */
@Service
@Transactional
public class ActiveServiceImpl implements ActiveService {

    @Resource
    private ActiveMapper activeMapper;


    @Override
    public ResponseResult activePage(Integer page, Integer limit, String publisher) {

        PageHelper.startPage(page, limit);

        List<Active> actives = activeMapper.getActives(publisher);

        PageInfo<Active> activePageInfo = new PageInfo<>(actives);
        long total = activePageInfo.getTotal();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("查询成功");
        responseResult.setCount(total);
        responseResult.setData(actives);

        return responseResult;
    }

    @Override
    public ResponseResult addActive(Active active) {

        activeMapper.addActive(active);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("活动发布成功");
        return responseResult;
    }

    @Override
    public ResponseResult getActiveById(Integer id) {

        Active active = activeMapper.selectById(id);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setData(active);
        return responseResult;
    }

    @Override
    public ResponseResult editActive(Active active) {

        activeMapper.updateActive(active);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("活动更新成功");
        return responseResult;
    }

    @Override
    public ResponseResult deleteActive(Integer[] ids) {

        activeMapper.deleteActive(ids);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("删除成功");
        responseResult.setCode(0);
        return responseResult;
    }

    @Override
    public ResponseResult activeList() {

        List<Active> actives = activeMapper.selectNewActive();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setData(actives);

        return responseResult;
    }
}
